import { connect, mapProps, mapReadPretty } from "@formily/react";
import { Button } from "antd";

const LinkButton = (props: any) => {
    let { value, href, target, disabled } = props
    return (
        <Button
            type={props.type || "link"}
            disabled={disabled}
            target={target}
            href={href}
        >
            {props.label || value}
        </Button>
    );
};

export const Link = connect(
    LinkButton,
    mapProps((props: any, field) => {
        return {
            ...props,
            target: props.target || "_blank",
        };
    }),
    mapReadPretty((props: any) => {
        let { label, value, type, clickFn, eventMap, disabled, target, href } = props
        return <Button type={type || "link"} disabled={disabled}
            target={target}
            href={href}
            onClick={clickFn && eventMap[clickFn] ? eventMap[clickFn] : () => { }}>
            {label || value}
        </Button>
    })
);

export default Link;
